﻿//var page_Sample = CreatePageControl('Sample', '/BaseStation/Page/?page=', 0, 509, 5);
//page_Sample.init();

//创建分页控件控制器
function CreatePageControl(controlId, url, page, recordCount, pageSize) {
    var oControl = new Object();
    oControl.controlId = controlId;
    oControl.pageSize = pageSize;
    oControl.currPageViewIndex = 0;
    oControl.pageCount = CalePageCount(recordCount, pageSize);
    oControl.pageViewCount = CalePageViewCount(oControl.pageCount);
    oControl.recordCount = recordCount;
    oControl.currPageViewIndex = 1;
    oControl.page = page;
    oControl.url = url;
    
    oControl.$$ = function(domId) {
        return document.getElementById(domId);
    }

    oControl.init = function() {
        if (recordCount == 0) {
            oControl.$$(controlId + '_pageControlsDiv').style.display = 'none';
            return;
        }
        var innerHtml = '<div class="total">共 <span class="red">' + oControl.recordCount + '</span>条记录</div>';

        innerHtml += '<a title="上十页" class="prev10" onclick="page_' + controlId + '.firstPageView()">&nbsp;</a>';
        innerHtml += '<a title="上一页" class="prev" onclick="page_' + controlId + '.prevPageView()">&nbsp;</a>';
        
        for (var i = 1; i <= this.pageViewCount; i++) {
            if (((i - 1) * 10 <= this.page) && (this.page < (i * 10))) {
                innerHtml += '<div id="' + controlId + 'pageView' + i + '">';
                this.currPageViewIndex = i;
            } else {
                innerHtml += '<div id="' + controlId + 'pageView' + i + '" style="display:none">';
            }
            for (var j = 0; j < 10; j++) {
                var pageIndex = (i - 1) * 10 + j;
                if (pageIndex > (this.pageCount - 1)) break;
                if (pageIndex == this.page)
                    innerHtml += "<strong>" + (pageIndex + 1) + "</strong>";
                else
                    innerHtml += "<a href=\"javascript:page_" + controlId + ".JunpToPage(" + pageIndex + ");\">" + (pageIndex + 1) + "</a>";
            }
            innerHtml += '</div>';
        }

        innerHtml += '<a title="下一页" class="next" onclick="page_' + controlId + '.nextPageView()">&nbsp;</a>';
        innerHtml += '<a title="下十页" class="next10" onclick="page_' + controlId + '.lastPageView()">&nbsp;</a>';

        oControl.$$(controlId + '_pageControlsDiv').innerHTML = innerHtml;
    }

    oControl.firstPageView = function() {
        if (this.currPageViewIndex == 1) return;
        oControl.$$(this.controlId + 'pageView' + this.currPageViewIndex).style.display = 'none';
        oControl.$$(this.controlId + 'pageView' + (--this.currPageViewIndex)).style.display = '';
    }

    oControl.prevPageView = function() {
        oControl.JunpToPage(oControl.page - 1);
    }

    oControl.nextPageView = function() {
        oControl.JunpToPage(oControl.page + 1);
    }

    oControl.lastPageView = function() {
        if (this.currPageViewIndex == this.pageViewCount) return;
        oControl.$$(this.controlId + 'pageView' + this.currPageViewIndex).style.display = 'none';
        oControl.$$(this.controlId + 'pageView' + (++this.currPageViewIndex)).style.display = '';
    }

    oControl.GoCustomPage = function() {
        var goPage = $("#" + this.controlId + "_customPageInput").val();
        this.JunpToPage(goPage);
    }

    oControl.JunpToPage = function(goPage) {
        if(goPage < 0) return;
        if(goPage >= oControl.pageCount) return;
        window.location.href = this.url + goPage;        
    }
    return oControl;
}

function GetDataGridSelectId(controlId) {
    var selectIds = new Array();
    $("input[name='" + controlId + "_record'][@checked]").each(function(i, o) {
        selectIds[i] = $(o).val();
    });
//    var idsCount = 0;
//    var selectCtls = document.getElementsByName(controlId + "_record");
//    for (var i = 0; i < selectCtls.length; i++) {
//        if (selectCtls[i].checked) {
//            selectIds[idsCount++] = selectCtls[i].value;
//        }
//    }
    return selectIds;
}

//DataGrid计算分页数
function CalePageCount(recordCount, pageSize) {
    return recordCount % pageSize == 0 ? recordCount / pageSize : Math.floor(recordCount / pageSize) + 1;
}

//DataGridPageView页试图数
function CalePageViewCount(pageCount) {
    return pageCount % 10 == 0 ? pageCount / 10 : Math.floor(pageCount / 10) + 1;
}